package com.xea.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;


import com.octo.captcha.service.CaptchaServiceException;
import com.xea.model.Online;
import com.xea.service.OnlineService;
import com.xea.service.ServiceFactory;
import com.xea.util.CaptchaServiceSingleton;
import com.xea.util.StringUtil;

public class OnlineAction extends DispatcherServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1932405126946046616L;
	
	
	/**
	 * 查询在线留言
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void search(HttpServletRequest request,HttpServletResponse response) throws Exception{
		OnlineService onlineDao = ServiceFactory.getOnlineService();
		String name=request.getParameter("name");
		if(StringUtil.isNullOrEmpty(name))
			name="";
		request.setAttribute("online", onlineDao.search(name));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/online/list.jsp");
		dispatcher.forward(request, response);	
	}
	
	/**
	 * 新增在线留言
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void add(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		OnlineService onlineDao = ServiceFactory.getOnlineService();
		response.setCharacterEncoding("GBK"); 
        String name=request.getParameter("name");
        String tel=request.getParameter("telphone");
        String email=request.getParameter("email");
        String content=request.getParameter("content");
        String msn=request.getParameter("msn");
        Boolean isResponseCorrect = Boolean.FALSE;
		String captchaId = request.getSession().getId();
		String vcode = request.getParameter("vcode");
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		try {
			isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, vcode);
			if(!isResponseCorrect){
				PrintWriter out = response.getWriter();
				json.put("msg", "Validation Code is not correct！");
				out.print(json.toString());
				out.close();
				return;
			}
		} catch (CaptchaServiceException e) {
			PrintWriter out = response.getWriter();
			json.put("msg", "Validation Code is not correct！");
			out.print(json.toString());
			out.close();
			return;
		}
        onlineDao.add(new Online(name, tel, email, content, new Date(),msn));
	}
	
	/**
	 * 查看在线留言
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void findById(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		OnlineService onlineDao = ServiceFactory.getOnlineService();
		String id=request.getParameter("id");
		request.setAttribute("online", onlineDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher(request.getParameter("_url"));
		dispatcher.forward(request, response);
	}
	
	/**
	 * 查看在线留言详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void detail(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		OnlineService onlineDao = ServiceFactory.getOnlineService();
		String id=request.getParameter("id");
		request.setAttribute("online", onlineDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/online/detail.jsp");
		dispatcher.forward(request, response);
	}
	
	/**
	 * 删除在线留言
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void del(HttpServletRequest request,HttpServletResponse response) throws IOException{
		OnlineService onlineDao = ServiceFactory.getOnlineService();
		String id=request.getParameter("id");
		onlineDao.del(id);
	}
	
	
}
